#-------------------------------------------------------------------------------
# Name:        Connect 4
# Purpose:
#
# Connect 4
# 7 columns, 6 rows
# X player 1, O player 2
#
# ctrl + F2 reset
#-------------------------------------------------------------------------------
#!/usr/bin/env python
from connect4board import *

import AI
import AIab
import AIabmonte

#Imports functions so use as AI.functionname()
def playPlayer():
    """Plays against human or otherwise. Controlled input"""
    print "Connect 4"
    gameBoard = Board()
    gameBoard.printBoard()
    gameover = 'no'
    currentPlayer = 'X'
    choice = raw_input("Do you want to go first? (y/n)") #X is human player, O is AI

    while gameover != 'yes':
        if choice == 'n':
            if currentPlayer == 'O':
                print 'Player', currentPlayer, 'choose a slot (0-6)'
                moveLegality = 'illegal'
                while 1:
                    #if 'legal' == gameBoard.playMove(currentPlayer,raw_input()):
                    moveLegality = gameBoard.playMove(currentPlayer,raw_input())
                    print moveLegality
                    if moveLegality == 'legal':
                        break
            if currentPlayer == 'X':
                gameBoard.playMove(currentPlayer,AI.Move(gameBoard, currentPlayer))

        if choice == 'y':
            if currentPlayer == 'X':
                print 'Player', currentPlayer, 'choose a slot (0-6)'
                moveLegality = 'illegal'
                while 1:
                    #if 'legal' == gameBoard.playMove(currentPlayer,raw_input()):
                    moveLegality = gameBoard.playMove(currentPlayer,raw_input())
                    print moveLegality
                    if moveLegality == 'legal':
                        break
            if currentPlayer == 'O':
                gameBoard.playMove(currentPlayer,AI.Move(gameBoard, currentPlayer))


        gameBoard.printBoard()

        #check if game has been won
        winner = gameBoard.winCheck()
        if winner == 'X':
            print "X has won"
            break
        if winner == 'O':
            print "O has won"
            break
        if winner == 'D':
            print "Game is Drawn"
            gameover = 'yes'
            break

        #switch to other player
        if currentPlayer == 'X':
            currentPlayer = 'O'
        else:
            currentPlayer = 'X'

def playSelf():
    print "Connect 4"
    gameBoard = Board()
    gameBoard.printBoard()
    gameover = 'no'
    currentPlayer = 'X'
    speedmode = 'y'
    #speedmode = raw_input("Do you want to only see who wins? (y/n)")

    while gameover != 'yes':
        if currentPlayer == 'O':
            gameBoard.playMove(currentPlayer,AIabmonte.Move(gameBoard, currentPlayer))

        if currentPlayer == 'X':
            gameBoard.playMove(currentPlayer,AIab.Move(gameBoard, currentPlayer))

        print "\nPlayer", currentPlayer, " has made a move below --"
        gameBoard.printBoard()

        #check if game has been won
        winner = gameBoard.winCheck()
        if winner == 'X':
            print "X has won"
            break
        if winner == 'O':
            print "O has won"
            break
        if winner == 'D':
            print "Game is Drawn"
            gameover = 'yes'
            break

        if speedmode == 'n':
            raw_input("enter to continue play")
        #switch to other player
        if currentPlayer == 'X':
            currentPlayer = 'O'
        else:
            currentPlayer = 'X'

def main():
    choose = raw_input("Bot vs bot? (y/n)")
    if choose == 'y':
        playSelf()
    else:
        playPlayer()

if __name__ == '__main__':
    main()
